<?php
/**
 * @param $num 数量
 * @param $amount 金额
 * @param $invalid_at 过期时间
 * @param int $start 开始
 * @param int $lenth 长度
 * @param string $batch 卡前缀
 * @param int $status
 * @return array|void
 */
function pn_host_server_make_card($num, $amount, $invalid_at, $start = 1, $lenth = 20, $batch = '', $status = 0)
{


    $ym = date('Ymd');

    if ($num == 0) return;
    $num = $num + $start; //卡的张数，即记录数
    $row = [];
    $start = 1;
    for ($i = $start; $i <= $num; $i++) {
        $seek = $ym . mt_rand(0, 9999) . mt_rand(0, 9999) . mt_rand(0, 9999) . mt_rand(0, 9999); //12位
        $start = 0;
        $str = strtoupper(substr(md5($seek), $start, $lenth));
        $str = str_replace("O", chr(mt_rand(65, 78)), $str);
        $str = str_replace("0", chr(mt_rand(65, 78)), $str);
        $row[] = [
            'card_number' => strtolower($str),
            'money' => $amount,
            'status' => $status,
            'created_at' => date('Y-m-d H:i:s'),
            'invalid_at' => $invalid_at
        ];

    }
    return $row;

}

/**
 * 统计虚拟数
 * @return array
 */
function pn_host_server_tongji()
{
    $model = \App\Plugin\HostServer\Models\PnHostSvNumber::checked()->sortDesc()->get();
    if (empty($model)) {
        return [];
    }
    $to_day = date('Y-m-d');
    $data = [];
    foreach ($model as $k => $v) {
        $data[$k] = $v->toArray();
        if ($v->to_day && $v->to_day == $to_day) {
            continue;
        } else {
            if ($v->type == 2) {
                continue;
            } else {
                $rand_number = explode('|', $v['range_number']);
                if (empty($rand_number)) {
                    continue;
                } else {
                    $rand_number = mt_rand($rand_number[0] ?? 0, $rand_number[1] ?? 10);
                    $v->number = $v->number + $rand_number;
                    $v->to_day = $to_day;
                    $v->save();
                    $data[$k]['number'] = $v->number;

                }

            }
        }
    }
    return $data;
}

/**
 * 取得单页
 * @param $type
 * @return mixed
 */
function pn_host_server_get_page($type)
{
    $pages = \App\Plugin\HostServer\Models\PnHostSvPage::where('group_type', $type)->checked()->sortDesc()->get()->toArray();
    return $pages;
}

/**
 * 取得导航
 * @return \Illuminate\Cache\CacheManager|mixed
 * @throws Exception
 */
function pn_host_server_nav($type = 'nav')
{
    $nav = cache('host_server_nav' . ($type ? '_' . $type : ''));
    if (empty($nav)) {
        $nav = \App\Plugin\Website\Models\PnHostSvNav::updateCache($type);
    }
    return $nav;
}

/**
 * 字符串
 * @param $str
 * @param $delimiter
 */
function pn_host_server_config_to_array($key, $delimiter='|')
{
    $str = config_cache_default('host_server.' . $key);
    if (empty($str)) {
        return [];
    }

    return explode($delimiter, $str);

}
//设置域名销售价
function pn_host_server_sale_price($system_price){
    $ratio = config_cache_default('host_server.domain_rate', 10);
    $add_price = round(bcmul($ratio, $system_price, 2) / 100, 2);
    return ceil($system_price+$add_price);//价格为系统价格
}


function get_area()
{
    return  \App\Models\ServerArea::getArea();
}

function getServerInfo()
{
    return  \App\Models\ServerTypeItem::getServerInfo();
}

function getProtectServer()
{
    return  \App\Models\ProtectServer::getProtectServer();
}

function getMonthFree()
{
    return  \App\Models\MonthFree::getMonthFree();
}


?>